import './assets/base.css'

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import 'element-plus/dist/index.css'
import http from './utils/http';
import { createPinia } from 'pinia'

import CallNotification from '@/components/CallNotification.vue'

import App from './App.vue'
import router from './router'

const app = createApp(App)
const pinia = createPinia()

// 路由导航守卫：控制登录权限
router.beforeEach((to, from, next) => {
    // 判断当前路由是否需要登录权限（在路由配置中通过 meta.requiresAuth 标记）
    const requiresAuth = to.meta.requiresAuth;
    // 从本地存储获取 token，判断是否已登录
    const token = localStorage.getItem('token');

    if (requiresAuth &&!token) {
        // 未登录且需要权限 → 跳转到登录页
        next('/Register'); // 假设登录页路由为 /login，根据实际路由调整
    } else {
        // 已登录或无需权限 → 正常访问
        next();
    }
});

app.use(router)
app.use(ElementPlus, { size: 'default', locale: zhCn })

// 注册图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

app.config.globalProperties.$http = http;
app.use(pinia)
app.mount('#app')